<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/2
 * Time: 13:56
 */
namespace Home\Controller;
use Think\Controller;

class TariffController extends Controller{
    public function index(){
        $post=I("post.");
        $chan ="and ";

        if(empty($post['channel_id'])){
            $chan="";
        }else{
            $chan.="q.id=".$post['channel_id'];
        }
        if(!empty($post['country_id'])){
           $scheme = M("weig_add")->alias("w")
                 ->join("tb_starting s on s.id=w.start_id")
                ->join("tb_partition_country pc on pc.id=s.pcid")
                ->join("tb_channel_partition cp on cp.id=s.cpid")
                ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                ->join("tb_channel q on q.id=cp.channel_id")
                ->join("tb_country c on c.id=pc.country_id")
               ->where("c.id={$post['country_id']} {$chan}")
               ->select();
            $g_pri=[];
            foreach($scheme as $key=>$val){
                foreach($val as $k=>$v){
                    $g_pri[$k][$key]=$v;
                }
            }
            array_multisort($g_pri["stage_min_weig"], constant(""), $scheme);
            $arr=[];
          /* foreach($scheme as $value) {
                if ($post['weight'] >= $value['start_weight']) {
                    if ($value['stage_min_weig'] <= $post['weight'] && $value['stage_max_weig'] >= $post['weight']) {
                        if(($post['weight'] - $value['start_weight']) % $value['add_weig']!=0){
                            $star = ($post['weight'] - $value['start_weight']) / $value['add_weig'];
                            $yun = $star * $value['add_weig_price'] + $value['start_fare'];
                        }else{
                            $yun = ($post['weight'] - $value['start_weight']) / $value['add_weig'] * $value['add_weig_price'] + $value['start_fare'];
                        }
                        $value['price'] = $yun * ($value["fuel"]/100+1);
                        $value['weight'] = $post['weight'];
                        $arr[] = $value;
                    }
                }elseif($value['stage_min_weig'] <= ceil($post['weight']) && $value['stage_max_weig'] >= ceil($post['weight'])){
                   if(($post['weight'] - $value['start_weight']) % $value['add_weig']!=0){
                       $star = ($post['weight'] - $value['start_weight']) / $value['add_weig'];
                       $yun = $star * $value['add_weig_price'] + $value['start_fare'];
                   }else{
                       $yun = ($post['weight'] - $value['start_weight']) / $value['add_weig'] * $value['add_weig_price'] + $value['start_fare'];
                   }
                   $value['price'] = $yun * ($value["fuel"]/100+1);
                   $value['weight'] = $post['weight'];
                   $arr[] = $value;
               }
            }*/
            foreach($scheme as $value) {
                if ($post['weight'] >= $value['start_weight']) {
                    if ($value['stage_min_weig'] <= $post['weight'] && $value['stage_max_weig'] >= $post['weight']) {
                        if($value['type']=0){//($get['weight'] - $value['start_weight']) % $value['add_weig']!=0
                            // $star = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']);
                            $yun = /*$star * $value['add_weig_price'] +*/ $value['start_fare'];
                        }else{
                            $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                        }
                        $value['price'] = $yun * ($value["fuel"]/100+1);
                        $value['weight'] = $post['weight'];
                        $arr[] = $value;
                    }
                }elseif($value['stage_min_weig'] <= ceil($post['weight']) && $value['stage_max_weig'] >= ceil($post['weight'])){
                    /*if(($get['weight'] - $value['start_weight']) % $value['add_weig']!=0){
                        $star = ($get['weight'] - $value['start_weight']) / $value['add_weig'];
                        $yun = $star * $value['add_weig_price'] + $value['start_fare'];*/
                    if($value['type']=0){
                        $yun = $value['start_fare'];
                    }else{
                        $yun = ceil(($post['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                    }
                    $value['price'] = $yun * ($value["fuel"]/100+1);
                    $value['weight'] = $post['weight'];
                    $arr[] = $value;
                }
            }

        }
        $state=M("country")->where("pid=0")->order("ename")->select();
        $chan =M("channel")->order("company")->select();
        $this->assign("arr",$arr);
        $this->assign("sta",$state);
        $this->assign("chan",$chan);
        $this->display();
    }
    public function ajax_tariff(){
        $get=I("get.");
        $chan ="and ";
        if(empty($get['channel_id'])){
            $chan="";
        }else{
            $chan.="q.id=".$get['channel_id'];
        }
        if(!empty($get['country_id'])) {
            $scheme = M("weig_add")->alias("w")
                ->join("tb_starting s on s.id=w.start_id")
                ->join("tb_partition_country pc on pc.id=s.pcid")
                ->join("tb_channel_partition cp on cp.id=s.cpid")
                ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                ->join("tb_channel q on q.id=cp.channel_id")
                ->join("tb_country c on c.id=pc.country_id")
                ->where("c.id={$get['country_id']} {$chan}")
                ->select();
            $g_pri=[];
            foreach($scheme as $key=>$val){
                foreach($val as $k=>$v){
                    $g_pri[$k][$key]=$v;
                }
            }
            array_multisort($g_pri["stage_min_weig"], constant(""), $scheme);
            $arr=[];

            foreach($scheme as $value) {
                if ($get['weight'] >= $value['start_weight']) {
                    if ($value['stage_min_weig'] <= $get['weight'] && $value['stage_max_weig'] >= $get['weight']) {
                        if($value['type']=0){//($get['weight'] - $value['start_weight']) % $value['add_weig']!=0
                           // $star = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']);
                            $yun = /*$star * $value['add_weig_price'] +*/ $value['start_fare'];
                        }else{
                            $yun = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                        }
                        $value['price'] = $yun * ($value["fuel"]/100+1);
                        $value['weight'] = $get['weight'];
                        $arr[] = $value;
                    }
                }elseif($value['stage_min_weig'] <= ceil($get['weight']) && $value['stage_max_weig'] >= ceil($get['weight'])&&$value['type']==1){
                    /*if(($get['weight'] - $value['start_weight']) % $value['add_weig']!=0){
                        $star = ($get['weight'] - $value['start_weight']) / $value['add_weig'];
                        $yun = $star * $value['add_weig_price'] + $value['start_fare'];*/
                    if($value['type']=0){
                        $yun = $value['start_fare'];
                    }else{
                        $yun = ceil(($get['weight'] - $value['start_weight']) / $value['add_weig']) * $value['add_weig_price'] + $value['start_fare'];
                    }
                    $value['price'] = $yun * ($value["fuel"]/100+1);
                    $value['weight'] = $get['weight'];
                    $arr[] = $value;
                }
            }

         }
        echo json_encode($arr);
    }

    public function _empty(){
        $this->display("Public:404");
    }
}